% Copyright (C) 2014-2022 Benjamin Born, Francesco D'Ascanio, Gernot J. Mueller, Johannes Pfeifer
%
% This is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This code is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% For a copy of the licencse,
% see <http://www.gnu.org/licenses/>.

%% Generate Table C.4: Pairwise correlations between government spending shocks within the EA sample

load('dataset_BDMP.mat')
% load('data_array_for_regression_stacked_by_variable_CDS_NE_OE')
temp=load('../Sign_restriction_first_stage_shocks_euro_area/results/structural_shocks_point.mat');

data_array_for_regression_stacked_by_variable = cat(3,data_array_for_regression_stacked_by_variable,temp.structural_shocks.BP_G,temp.structural_shocks.BP_T,temp.structural_shocks.PF_G,temp.structural_shocks.PF_T);
pos.BP_shock_G_CK=length(fieldnames(pos))+1;
Header{1,pos.BP_shock_G_CK}='BP_shock G based on Caldara/Kamps';
pos.BP_shock_T_CK=length(fieldnames(pos))+1;
Header{1,pos.BP_shock_T_CK}='BP_shock T based on Caldara/Kamps';
pos.PF_shock_G=length(fieldnames(pos))+1;
Header{1,pos.PF_shock_G}='Sign restriction shock G based on Caldara/Kamps';
pos.PF_shock_T=length(fieldnames(pos))+1;
Header{1,pos.PF_shock_T}='Sign restriction shock T based on Caldara/Kamps';


shocks=squeeze(data_array_for_regression_stacked_by_variable(:,:,pos.PF_shock_G));
timeline=squeeze(data_array_for_regression_stacked_by_variable(:,:,pos.timeline));
EU_indicator=squeeze(data_array_for_regression_stacked_by_variable(end,:,pos.euro_country))==1;


shock_mat_EU=shocks(:,EU_indicator);
country_header_EU=country_header(EU_indicator);

NaN_countries=find(all(isnan(shock_mat_EU),1));
shock_mat_EU(:,NaN_countries)=[];
country_header_EU(NaN_countries)=[];


correlations=corr(shock_mat_EU,'Rows','pairwise');
correlations=correlations+tril(NaN(size(correlations)),-1);
coeff_table = array2table(correlations,'VariableNames',country_header_EU);

% for syntax, see latexTable example in Data/Data/Auxiliary_Files 
input.transposeTable = 0;
input.dataNanString = '-';
input.tableColumnAlignment = 'l';
input.tableBorders = 0;
input.booktabs = 1;

input.data = correlations;
input.tableRowLabels = country_header_EU;
input.tableColLabels = country_header_EU;
input.dataFormat = {'%1.2f'};
input.tableCaption = 'Correlation';
input.tableLabel = 'Correlation';

input.makeCompleteLatexDocument = 0;
latex = latexTable(input);